#I Import and Initialize
import  sys, os
import pygame
import random
from sprites import *
pygame.init()
size = width, height = 800, 600

#D Display
screen = pygame.display.set_mode(size) #retourneert Surface
pygame.display.set_caption("Revenge of the Sh*t")


#E Entities
background1 = loadImage("background.png") #zet background
background2 = loadImage("background.png")
bgrect1     = background1.get_rect() #retourneert BG Rect
bgrect2     = background2.get_rect()
#clist      = pygame.camera.list_cameras()

class Camera(object):
    def __init__(self, player, rect):
        self.player = player
        self.rect   = rect
        self.width  = width
        self.height = height
        
    def update(self):
        if self.player.rect.right >= self.rect.width and player.x_velocity > 0:
            player.x_velocity = 0
        if self.player.rect.left <= self.rect.left and player.x_velocity < 0:
            player.x_velocity = 0
        if self.player.rect.top <= self.rect.top and player.y_velocity < 0:
            player.y_velocity = 0
        if self.player.rect.bottom >= self.rect.height and player.y_velocity > 0:
            player.y_velocity = 0
            
        self.rect.clamp_ip(self.rect)
    
    def draw(self, surf, playerSprite):
        subsurface = surf.subsurface(self.rect)
        playerSprite.draw(subsurface)

#A Action onderverdeeld in ALTER

#A Assign
playerSprite    = pygame.sprite.RenderClear()
player          = Player(10, 60)
playerSprite.add(player)

speed       = [4, 4]
bgspeed     = [-3,0]
white       = 255, 255, 255
clock       = pygame.time.Clock()
keepGoing   = True
bgrect1     = bgrect1.move(0, 0)
bgrect2     = bgrect2.move(width, 0)
camera      = Camera(player, screen.get_rect())

#L Loop
while keepGoing:
    #T Timer (framerate)
    clock.tick(90)
        
    #E Event
    bgrect1 = bgrect1.move(bgspeed) # bewegende achtergrond
    bgrect2 = bgrect2.move(bgspeed)
    if bgrect1.right < 0:
        bgrect1 = bgrect1.move((width*2, 0))
    if bgrect2.right < 0:
        bgrect2 = bgrect2.move((width*2, 0))

    for event in pygame.event.get():
            if event.type == pygame.QUIT:
                keepGoing = False
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    keepGoing = False
                elif event.key == pygame.K_LEFT:
                    player.x_velocity = -4
                elif event.key == pygame.K_RIGHT:
                    player.x_velocity = 4
                elif event.key == pygame.K_UP:
                    player.y_velocity = -4
                elif event.key == pygame.K_DOWN:
                    player.y_velocity = 4
                
            elif event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT:
                    player.x_velocity = 0
                elif event.key == pygame.K_RIGHT:
                    player.x_velocity = 0
                elif event.key == pygame.K_UP:
                    player.y_velocity = 0
                elif event.key == pygame.K_DOWN:
                    player.y_velocity = 0

    #R refresh
    camera.update()
    player.update()
    screen.fill(white)
    screen.blit(background1, bgrect1)
    screen.blit(background2, bgrect2)
    camera.draw(screen, playerSprite)
    pygame.display.flip()
